/**
 * 
 */
package org.t2.backstage.util;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.gaming.ruler.util.HttpUtil;
import org.gaming.service.advice.ResponseResult;
import org.gaming.service.asserts.Asserts;
import org.gaming.tool.GsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.t2.backstage.module.TipsCode;
import org.t2.backstage.module.gameserver.model.GameServer;

import com.google.gson.reflect.TypeToken;

/**
 * @author YY
 *
 */
public class T2HttpUtil {
	
	private static Logger logger = LoggerFactory.getLogger(T2HttpUtil.class);
	
	public static ResponseResult<String> get(GameServer gameServer, String url) {
		String httpUrl = "http://" + gameServer.getInternalIp() + ":" + gameServer.getHttpPort() + url;
		try {
			String response = HttpUtil.get(httpUrl);
			if(response == null) {
				return null;
			}
			return GsonUtil.parseJson(response, TypeToken.getParameterized(ResponseResult.class, String.class));
		} catch (Exception e) {
			logger.error("请求" + httpUrl + "发生异常", e);
			return null;
		}
	}
	
	public static ResponseResult<String> formPost(GameServer gameServer, String url, Map<String, Object> parameter) {
		String httpUrl = "http://" + gameServer.getInternalIp() + ":" + gameServer.getHttpPort() + url;
		try {
			String response = HttpUtil.formPost(httpUrl, parameter);
			if(response == null) {
				return null;
			}
			return GsonUtil.parseJson(response, TypeToken.getParameterized(ResponseResult.class, String.class));
		} catch (Exception e) {
			logger.error("请求" + httpUrl + "发生异常", e);
			return null;
		}
	}
	
	public static <T> T simplePost(GameServer gameServer, String url, Map<String, Object> parameter, Class<T> clazz) {
		String httpUrl = "http://" + gameServer.getInternalIp() + ":" + gameServer.getHttpPort() + url;
		try {
			String response = HttpUtil.formPost(httpUrl, parameter);
			Asserts.isTrue(response != null, TipsCode.POST_NO_RESPONSE, url);
			ResponseResult<T> serverResponse = GsonUtil.parseJson(response, TypeToken.getParameterized(ResponseResult.class, clazz));
			Asserts.isTrue(serverResponse.getCode() == 0, TipsCode.POST_FAIL, url);
			return (T)serverResponse.getData();
		} catch (Exception e) {
			logger.error("请求" + httpUrl + "发生异常", e);
			return null;
		}
	}
	
	public static <T> ResponseResult<T> jsonPost(GameServer gameServer, String url, Object parameter, Class<T> clazz) {
		String httpUrl = "http://" + gameServer.getInternalIp() + ":" + gameServer.getHttpPort() + url;
		try {
			String response = HttpUtil.jsonPost(httpUrl, parameter);
			if(response == null) {
				return null;
			}
			return GsonUtil.parseJson(response, TypeToken.getParameterized(ResponseResult.class, clazz));
		} catch (Exception e) {
			logger.error("请求" + httpUrl + "发生异常", e);
			return null;
		}
	}
	
	
	public static ResponseResult<String> postParamAndFile(GameServer gameServer, String url, Map<String, Object> params, List<File> files) {
		String httpUrl = "http://" + gameServer.getInternalIp() + ":" + gameServer.getHttpPort() + url;
		try {
			String response = HttpUtil.postParamAndFile(httpUrl, params, files);
			if(response == null) {
				return null;
			}
			return GsonUtil.parseJson(response, TypeToken.getParameterized(ResponseResult.class, String.class));
		} catch (Exception e) {
			logger.error("请求" + httpUrl + "发生异常", e);
			return null;
		}
	}
}
